package 生产者消费者;

public class Resource {

    private int[] arr=new int[5];
    private int count=0;

    synchronized public void producer(){
        try{
            while (count==arr.length){
                wait();
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        int m= (int) Math.floor(Math.random()*10);
        arr[count]=m;
        count++;
        System.out.println("生产："+m);
        notifyAll();

    }
    synchronized public void consumer(){
        try{
            while (count==0){
                wait();
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        int m= arr[count-1];
        count--;
        System.out.println("消费："+m);
        notifyAll();

    }



}
